﻿/*
 实现个人中心接口请求的代码实现
 */

using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TopVisionLog;

namespace TopVisionVideoStream.TopVisionCenter
{
    /// <summary>
    /// 个人中心接口请求
    /// </summary>
    public class TVCenter
    {

        #region Base Url

        private TVHttpRequest httpRequest = new TVHttpRequest();


        /// <summary>
        /// 阿里云
        /// http://api.topvision-cv.com:8082
        /// 内网1
        /// http://192.168.2.70:8088
        /// 内网2
        /// http://192.168.2.71:8088
        /// 南京云
        /// http://112.80.35.3:9080/
        /// 河北国网
        /// http://120.211.156.219:82
        /// 钜星
        /// http://180.167.253.166:9001
        /// </summary>
        public static string BaseHost = @"http://api.topvision-cv.com:8082";

        public string BaseApi = @"/topvision-api";

        #endregion


        #region Api-Model

        public string VideoModel = @"/video";

        #endregion

        public TVCenter()
        {

        }

        #region Api

        #region 用户登录

        /// <summary>
        /// 用户登录
        /// </summary>
        public string VideoUserLogin = @"/user/login";
        public string VideoUserLoginApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoUserLogin);

        public async Task<TVCenterResponse> UserLogin(TVCUserLoginModel model)
        {
            try
            {
                if (model == null) throw new Exception("user model is null");

                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoUserLoginApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                Console.WriteLine("TVC UserLogin:" + ex.Message);
                return new TVCenterResponse() { code = TVCenterResponse_Code.Failed, error = "post error" };
            }
        }


        /// <summary>
        /// 查询企业密码
        /// </summary>
        public string VideoQuerycompanyinfo = @"/querycompanyinfo";
        public string VideoQuerycompanyinfoApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoQuerycompanyinfo);

        public async Task<TVCenterResponse> Querycompanyinfo(TVCQuerycompanyinfoModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoQuerycompanyinfoApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "Querycompanyinfo", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }



        #endregion


        #region 联系人

        /// <summary>
        /// 联系人
        /// </summary>
        public string VideoConferencesUsers = @"/conferences/users";
        public string VideoConferencesUsersApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoConferencesUsers);

        /// <summary>
        /// 联系人
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<TVCenterResponse> ConferencesUsers(TVCConferencesUsersModel model)
        {
            TVCenterResponse response = null;

            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoConferencesUsersApi, json);

                response = JsonMapper.ToObject<TVCenterResponse>(result);
            }
            catch (Exception ex)
            {
                TVLog.WriteLine("ERR ConferencesUsers " + ex.Message);

                return new TVCenterResponse() { success = false };
            }

            return response;
        }

        #endregion


        #region 会议

        /// <summary>
        /// 发起会议
        /// </summary>
        public string VideoConferenceCall = @"/conference/call";
        public string VideoConferenceCallApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoConferenceCall);

        public async Task<TVCenterResponse> ConferenceCall(TVCConferenceCallModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoConferenceCallApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "ConferenceCall", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }


        /// <summary>
        /// 视频会议控制-增加参与方
        /// </summary>
        public string VideoConferenceConfctrladd = @"/conferences/confctrladd";
        public string VideoConferenceConfctrladdApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoConferenceConfctrladd);

        public async Task<TVCenterResponse> ConferenceConfctrladd(TVCConferencesConfctrladdModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoConferenceConfctrladdApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "ConferenceConfctrladd", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }


        /// <summary>
        /// 视频会议控制-减少参与方
        /// </summary>
        public string VideoConferenceConfctrldel = @"/conferences/confctrldel";
        public string VideoConferenceConfctrldelApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoConferenceConfctrldel);

        public async Task<TVCenterResponse> ConferenceConfctrldel(TVCConferencesConfctrldelModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoConferenceConfctrldelApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "ConferenceConfctrldel", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }


        /// <summary>
        /// 视频会议控制-结束会议
        /// </summary>
        public string VideoConferenceConfctrlhalt = @"/conferences/confctrlhalt";
        public string VideoConferenceConfctrlhaltApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoConferenceConfctrlhalt);

        public async Task<TVCenterResponse> ConferenceConfctrlhalt(TVCConferencesConfctrlhaltModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoConferenceConfctrlhaltApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "ConferenceConfctrlhalt", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }


        /// <summary>
        /// 视频会议控制-查询会议成员状态
        /// </summary>
        public string VideoConferenceGetconfmemberinfo = @"/conferences/getconfmemberinfo";
        public string VideoConferenceGetconfmemberinfoApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoConferenceGetconfmemberinfo);

        public async Task<TVCenterResponse> ConferenceGetconfmemberinfo(TVCConferencesConfGetconfmemberinfoModel model)
        {
            try
            {
                JsonData jd = new JsonData();

                jd["companyid"] = model.companyid;

                jd["confid"] = model.confid;

                if (!string.IsNullOrEmpty(model.status))
                    jd["statu"] = model.status;

                jd["cookie"] = model.cookie;

                string json = JsonMapper.ToJson(jd);

                var result = await httpRequest.PostAsync(VideoConferenceGetconfmemberinfoApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "ConferenceGetconfmemberinfo", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }


        /// <summary>
        /// 查询单个会议信息
        /// </summary>
        public string VideoConferenceGetconfinfobyid = @"/conferences/getconfinfobyid";
        public string VideoConferenceGetconfinfobyidApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoConferenceGetconfinfobyid);

        public async Task<TVCenterResponse> ConferenceGetconfinfobyid(TVCConferenceGetconfinfobyidModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoConferenceGetconfinfobyidApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "ConferenceGetconfinfobyid", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }


        /// <summary>
        /// 查询会议状态
        /// </summary>
        public string VideoConferenceGetconfinfo = @"/conferences/getconfinfo";
        public string VideoConferenceGetconfinfoApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoConferenceGetconfinfo);

        public async Task<TVCenterResponse> ConferenceGetconfinfo(TVCConferenceGetconfinfoModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoConferenceGetconfinfoApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "ConferenceGetconfinfo", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }


        /// <summary>
        /// 视频会议控制-控制静音哑音
        /// </summary>
        public string VideoConferenceConfsilence = @"/conferences/confsilence";
        public string VideoConferenceConfsilenceApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoConferenceConfsilence);

        public async Task<TVCenterResponse> ConferenceConfsilence(TVCConferenceConfsilenceModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoConferenceConfsilenceApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "ConferenceConfsilence", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }


        /// <summary>
        /// 视频会议控制-设置画面布局
        /// </summary>
        public string VideoConferenceConfscreen = @"/conferences/confscreen";
        public string VideoConferenceConfscreenApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoConferenceConfscreen);

        public async Task<TVCenterResponse> ConferenceConfscreen(TVCConferenceConfscreenModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoConferenceConfscreenApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "ConferenceConfscreen", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }


        /// <summary>
        /// 视频会议控制-控制播放视频文件
        /// </summary>
        public string VideoConferenceConfplayfile = @"/conferences/confplayfile";
        public string VideoConferenceConfplayfileApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoConferenceConfplayfile);

        public async Task<TVCenterResponse> ConferenceConfplayfile(TVCConferenceConfplayfileModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoConferenceConfplayfileApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "ConferenceConfplayfile", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }


        /// <summary>
        /// 视频会议控制-视频会议广播消息
        /// </summary>
        public string VideoConferenceConfmsg = @"/conferences/confmsg";
        public string VideoConferenceConfmsgApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoConferenceConfmsg);

        public async Task<TVCenterResponse> ConferenceConfmsg(TVCConferenceConfmsgModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoConferenceConfmsgApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "ConferenceConfmsg", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }


        /// <summary>
        /// 视频会议控制-设置大画面
        /// </summary>
        public string VideoConferenceConfctrlview = @"/conferences/confctrlview";
        public string VideoConferenceConfctrlviewApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoConferenceConfctrlview);

        public async Task<TVCenterResponse> ConferenceConfctrlview(TVCConferenceConfctrlviewModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoConferenceConfctrlviewApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "ConferenceConfctrlview", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }


        /// <summary>
        /// 会议房间
        /// </summary>
        public string VideoConferenceQueryrooms = @"/conferences/queryrooms";
        public string VideoConferenceQueryroomsApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoConferenceQueryrooms);

        public async Task<TVCenterResponse> ConferenceQueryrooms(TVCConferenceQueryroomsModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoConferenceQueryroomsApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "ConferenceQueryrooms", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }

        #endregion


        #region 视频

        /// <summary>
        /// 回看视频
        /// </summary>
        public string VideoConferenceVideos = @"/conferences/videos";
        public string VideoConferenceVideosApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoConferenceVideos);

        public async Task<TVCenterResponse> ConferenceVideos(TVCConferenceVideosModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoConferenceVideosApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "ConferenceVideos", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }

        /// <summary>
        /// 回看视频2
        /// </summary>
        public string VideoConferenceVideos2 = @"/conferences/videos2";
        public string VideoConferenceVideosApi2 => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoConferenceVideos2);

        public async Task<HttpAPIResponse> ConferenceVideos2(TVCConferenceVideosModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoConferenceVideosApi2, json);

                var response = JsonMapper.ToObject<HttpAPIResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "ConferenceVideos2", ex.Message));
                return new HttpAPIResponse() { success = false };
            }
        }

        #endregion


        #region 管理

        /// <summary>
        /// 上传文件
        /// </summary>
        public string VideoManageUploadfile = @"/manage/uploadfile";
        public string VideoManageUploadfileApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoManageUploadfile);

        public async Task<TVCenterResponse> ConferenceVideos(TVCManageUploadfileModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoManageUploadfileApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "ConferenceVideos", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }


        public string _VersionCheck = @"/version/check";

        public string VersionCheckApi => string.Format("{0}{1}{2}", BaseHost, BaseApi, _VersionCheck);

        public async Task<TVCenterResponse> VersionCheck(TVCManagerVersionCheckModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VersionCheckApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "VersionCheck", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }


        public string VideoInsertvideodata = @"/insertvideodata";
        public string VideoInsertvideodataApi => string.Format("{0}{1}{2}{3}", BaseHost, BaseApi, VideoModel, VideoInsertvideodata);
        public async Task<TVCenterResponse> Insertvideodata(TVCManagerInsertvideodataModel model)
        {
            try
            {
                string json = JsonMapper.ToJson(model);

                var result = await httpRequest.PostAsync(VideoInsertvideodataApi, json);

                var response = JsonMapper.ToObject<TVCenterResponse>(result);

                return response;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(string.Format("{0} {1}_{2}", this.GetType().Name, "Insertvideodata", ex.Message));
                return new TVCenterResponse() { success = false };
            }
        }

        #endregion


        #endregion

    }
}
